package com.atheima.reggie.controller;

import com.atheima.reggie.common.R;
import com.atheima.reggie.common.ValidateUtil;

import com.atheima.reggie.entity.Category;
import com.atheima.reggie.entity.Employee;
import com.atheima.reggie.service.CategoryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;

/**
 * 分类管理
 */
@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @PostMapping("")
    public R save(@RequestBody Category category) {
        categoryService.save(category);
        return R.ok();
    }

    @GetMapping("/page")
    public R savpagee(int page,int pageSize) {
        Page<Category> employeePage = new Page<>(page, pageSize);
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("sort");
        categoryService.page(employeePage, wrapper);
        return R.ok("成功").put("data",employeePage);
    }

    @GetMapping("/list")
    public R list(Category category) {
        Integer type = category.getType();

        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        if(type != null) {
            wrapper.eq("type",type);
        }
        wrapper.orderByAsc("sort");
        List<Category> list = categoryService.list(wrapper);

        return R.ok("成功").put("data",list);
    }

    @DeleteMapping("")
    public R deleteById(Long ids) {
        categoryService.removeById(ids);
        return R.ok();
    }

    @PutMapping("")
    public R update(@RequestBody Category category) {
        categoryService.updateById(category);
        return R.ok();
    }
}
